//
//  MeViewController.m
//  HxgProject
//
//  Created by hxg on 2018/3/1.
//  Copyright © 2018年 XMYY-19. All rights reserved.
//

#import "MeViewController.h"
#import "CustomBtn.h"
#import "JiLuVC.h"

#import "MeService.h"
#import "ApplyInfo.h"
#import "JinDuVC.h"

@interface MeViewController ()<UITableViewDelegate,UITableViewDataSource>

// 个人中心的表格
@property (nonatomic, strong) UITableView *meTView;

//  数据源
@property (nonatomic, strong) NSArray *meDataSource;

// 图片数据源
@property (nonatomic, strong) NSArray *meImgsArr;

// 详细的支付信息
@property (nonatomic, strong) NSMutableArray *allApplyArrs;

@end

@implementation MeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.allApplyArrs = [[NSMutableArray alloc] init];
    
    [self setUpUI];
    
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self loadData];
}

#pragma mark - getter、setter
- (UITableView *)meTView
{
    if (!_meTView)
    {
        _meTView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _meTView.dataSource = self;
        _meTView.delegate = self;
        _meTView.tableFooterView = [[UIView alloc] init];
    }
    return _meTView;
}

- (NSArray *)meDataSource
{
    if (!_meDataSource)
    {
        NSArray *arr1 = [NSArray arrayWithObjects:@"进度查询",@"申请记录",@"还款查询", nil];
        NSArray *arr2 = [NSArray arrayWithObjects:@"退出登陆", nil];
        _meDataSource = [NSArray arrayWithObjects:arr1,arr2, nil];
    }
    return _meDataSource;
}

- (NSArray *)meImgsArr
{
    if (!_meImgsArr)
    {
        NSArray *arr1 = [NSArray arrayWithObjects:@"process",@"jilu",@"huankuan", nil];
        NSArray *arr2 = [NSArray arrayWithObjects:@"logout", nil];
        _meImgsArr = [NSArray arrayWithObjects:arr1,arr2, nil];
    }
    return _meImgsArr;
}


- (void)loadData
{
    __weak MeViewController *weakSelf = self;
    MeService *meService = [[MeService alloc] init];
    [meService getPackageDetailWithCallBack:^(NSMutableArray *applyArr) {
                                weakSelf.allApplyArrs = applyArr;
                            }];
}


- (void)setUpUI{
    
    self.navigationItem.title = @"我的";
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont systemFontOfSize:19.0f],
       NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#FFFFFF"]}];
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:68.0f/255.0f green:161.0f/255.0f blue:247.0f/255.0f alpha:1.0f];

    
    [self.view addSubview:self.meTView];
    [self.meTView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(64.0f);
        make.left.equalTo(self.view.mas_left);
        make.size.mas_equalTo(CGSizeMake(self.view.frame.size.width, self.view.frame.size.height - 64.0f - 44.0f));
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDelegate
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.imageView.image = [UIImage imageNamed:self.meImgsArr[indexPath.section][indexPath.row]];
    cell.textLabel.text = self.meDataSource[indexPath.section][indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70.0f;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *rowsArr = self.meDataSource[section];
    
    return rowsArr.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.meDataSource.count;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (0 == indexPath.section)
    {
        if (0 == indexPath.row  ) {
            JinDuVC *jinDu  = [[JinDuVC alloc] init];
            jinDu.showTitleStr = @"进度查询";
            
            NSMutableArray *arr = [[NSMutableArray alloc] init];
            for (ApplyInfo *appInfo in self.allApplyArrs) {
                if (![appInfo.curState isEqualToString:@"2"])
                {
                    [arr addObject:appInfo];
                }
            }
            
            jinDu.detailApplyArr = arr;
            [self.navigationController pushViewController:jinDu animated:YES];
        } else if (1 == indexPath.row) {
            JiLuVC *jiluVC  = [[JiLuVC alloc] init];
            jiluVC.showTitleStr = @"申请记录";
            jiluVC.detailApplyArr = self.allApplyArrs;
            [self.navigationController pushViewController:jiluVC animated:YES];
        }
        else if (2 == indexPath.row)
        {
            JinDuVC *jinDu  = [[JinDuVC alloc] init];
            jinDu.showTitleStr = @"还款查询";
            jinDu.detailApplyArr = self.allApplyArrs;
            [self.navigationController pushViewController:jinDu animated:YES];
        }
    }
    else if (1 == indexPath.section
        && 0 == indexPath.row)
    {
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示"
                                                                         message:@"确定退出"
                                                                  preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定"
                                                           style:UIAlertActionStyleDestructive
                                                         handler:^(UIAlertAction * _Nonnull action) {
                                                             // 情况缓存信息
                                                             [[NSUserDefaults standardUserDefaults] removeObjectForKey:kPhoneNum];
                                                             [[NSUserDefaults standardUserDefaults] removeObjectForKey:kContactAuthenRes];
                                                             [[NSUserDefaults standardUserDefaults] removeObjectForKey:kPersonInfoAuthenRes];
                                                             [[NSUserDefaults standardUserDefaults] removeObjectForKey:kXinYuAuthenRes];
                                                             
                                                             
                                                             
                                                             [self dismissViewControllerAnimated:YES completion:nil];
                                                         }];
        [alertVC addAction:cancelAction];
        [alertVC addAction:okAction];
        [self presentViewController:alertVC animated:YES completion:nil];
    }
    
}

#pragma mark - getter、setter




@end
